package com.yq.day71;

/**
 * @author 3307235355@qq.com
 * @description
 * @since 2022/07/01 19:36
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * /* 完成如下方法，要求该方法能调用，指定配置文件中，
 * 指定类中指定的普通成员方法(无参方法) 假设配置文件中有className
 * 和 methodName 这2个属性 假设指定类中一定有默认构造方法

 */
public class Demo02 {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InstantiationException, IOException, InvocationTargetException, ClassNotFoundException {
        Demo02 demo02 = new Demo02();
        demo02.callTargetMethod("D:\\IDEAwork\\day02\\practice\\src\\com\\yq\\day71\\config.properties");

    }

    public void callTargetMethod(String configFilePath) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Properties properties = new Properties();
        properties.load(new FileInputStream(configFilePath));
        //获取属性值
        Object className = properties.get("className");
        Object methodName = properties.get("methodName");

        //获取字节码文件对象
        Class<?> aClass = Class.forName((String) className);

        // Class<?> aClass = className.getClass();
        // Object o = aClass.newInstance();

        // 获取指定的构造方法
        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class, int.class);

        //使用构造方法创建对象
        Object o = declaredConstructor.newInstance("张三",15);

        //获取对象方法
        Method method = aClass.getMethod((String) methodName);

        System.out.println(o);
        //方法的调用
        System.out.println("方法的调用");
        method.invoke(o);


    }

}
